package com.jcn.seckill.util;

import org.apache.commons.codec.digest.DigestUtils;

public class MD5Util {
	//MD5
	public static String md5(String src) {
		return DigestUtils.md5Hex(src);
	}
	
	//固定salt，写死客户端，如果是随机的话，服务器端不知道salt是什么东西 
	private static final String salt = "1a2b3c4d";
	
	//用户端
	//第一层MD5:用户输入的密码拼上salt后，再来一次MD5转化成formMD5
	public static String inputPassToFormPass(String inputPass) {
		String str = ""+salt.charAt(0)+salt.charAt(2) + inputPass +salt.charAt(5) + salt.charAt(4);
		System.out.println(str);
		return md5(str);
	}
	//第二层MD5
	//明文加固定salt
	public static String formPassToDBPass(String formPass, String salt) {
		String str = ""+salt.charAt(0)+salt.charAt(2) + formPass +salt.charAt(5) + salt.charAt(4);
		return md5(str);
	}
	
	//服务端
	//第二层MD5：将form表单明文密码转换为DB密码
	public static String inputPassToDbPass(String inputPass, String saltDB) {
		String formPass = inputPassToFormPass(inputPass);
		String dbPass = formPassToDBPass(formPass, saltDB);
		System.out.println("dbpass:" + dbPass);
		return dbPass;
	}
	
	public static void main(String[] args) {
		System.out.println(inputPassToFormPass("123456"));//d3b1294a61a07da9b49b6e22b2cbd7f9
		System.out.println(formPassToDBPass(inputPassToFormPass("123456"), "1a2b3c4d"));
		System.out.println(inputPassToDbPass("123456", "1a2b3c4d"));//b7797cce01b4b131b433b6acf4add449
	}
	
}

